package cn.breeze.user.controller;

import cn.breeze.basic.annotation.PreAuthorize;
import cn.breeze.basic.vo.RegisterVo;
import cn.breeze.user.service.IUserService;
import cn.breeze.user.domain.User;
import cn.breeze.user.query.UserQuery;
import cn.breeze.basic.utils.AjaxResult;
import cn.breeze.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.breeze.basic.exception.BusinessException;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private  IUserService userService;


    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存用户", sn = "user:save")
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.save(user);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除用户", sn = "user:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "获取用户", sn = "user:get")
    public User get(@PathVariable("id")Long id)
    {
        return userService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "获取所有用户", sn = "user:loadAll")
    public List<User> list(){

        return userService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "列表用户", sn = "user:list")
    public PageList<User> queryData(@RequestBody UserQuery query)
    {
        return userService.queryData(query);
    }

    /**
    * @Title: delete
    * @Description: 用户手机号注册
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/6 11:51
    * @Parameters: [id]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping(value="/register/phone")
    @PreAuthorize(name = "手机注册用户", sn = "user:register/phone")
    public AjaxResult phoneRegister(@RequestBody RegisterVo registerVo){
        try {
            userService.phoneRegister(registerVo);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }

    /**
    * @Title: phoneRegister
    * @Description: 邮箱注册
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/7 18:22
    * @Parameters: [registerVo]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping(value="/register/email")
    @PreAuthorize(name = "邮箱注册用户", sn = "user:register/email")
    public AjaxResult emailRegister(@RequestBody RegisterVo registerVo){
        try {
            userService.emailRegister(registerVo);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }
}
